﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Z2.Core
{
    /// <summary>
    /// PixelShader Blur effect
    /// </summary>
    public class BlurEffect : EffectBase
    {

        #region Const

        private const string Asset = "Graphics/Blur";

        #endregion

        #region Members

        private Effect m_blurEffect;
        private ResolveTexture2D m_resolveTex;
        private Rectangle m_rect;
        private Vector2 m_intensity;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="content"><see cref="ContentManager"/> containing effects</param>
        /// <param name="graphics"><see cref="GraphicsDeviceManager"/></param>
        public BlurEffect(ContentManager content, GraphicsDeviceManager graphics)
            : base(content, graphics)
        {
            this.m_blurEffect = this.Content.Load<Effect>(BlurEffect.Asset); 
            this.m_resolveTex = new ResolveTexture2D(this.Graphics.GraphicsDevice,
                this.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
                this.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight, 1,
                this.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat);
            Viewport viewport = this.Graphics.GraphicsDevice.Viewport;
            this.m_rect = new Rectangle(0, 0, viewport.Width, viewport.Height);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets blur intensity
        /// </summary>
        public Vector2 Intensity
        {
            get { return this.m_intensity; }
            set { this.m_intensity = value; }
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Draws effect
        /// </summary>
        /// <param name="rect">Not used in this effect</param>
        /// <param name="batch"><see cref="SpriteBatch"/></param>
        /// <param name="gameTime"><see cref="GameTime"/></param>
		public override void Draw(SpriteBatch batch, GameTime gameTime)
		{
			batch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

			this.Graphics.GraphicsDevice.ResolveBackBuffer(this.m_resolveTex);

			this.m_blurEffect.Parameters["BlurDistance"].SetValue(this.m_intensity);

			this.m_blurEffect.Begin();
			this.m_blurEffect.CurrentTechnique.Passes[0].Begin();
			batch.Draw(this.m_resolveTex, this.m_rect, this.m_rect, Color.White);
			this.m_blurEffect.CurrentTechnique.Passes[0].End();
			this.m_blurEffect.End();
			batch.End();
        }

        #endregion

    }
}
